﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Dapper;
using TaxBusiness.Model;

namespace TaxBusiness.DAL
{
    public class BusinessTypeDB
    {
        private string _insert = "INSERT INTO [tb_BusinessType] " +
                                      "([TypeName]" +
                                       ",[ParentID])" +
                                 " VALUES " +
                                       "(@TypeName" +
                                       ",@ParentID)";
        private string _delete = "DELETE FROM [tb_BusinessType] WHERE ID=@ID";
        private string _select = "SELECT * FROM [tb_BusinessType] WHERE ParentID=0";
        private string _selectChild = "SELECT * FROM [tb_BusinessType] WHERE ParentID=@ParentID";

        public int Insert(Model.BusinessType businesstype)
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Execute(_insert, businesstype);
            }
        }

        public int Delete(int id)
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Execute(_delete, new { ID = id });
            }
        }

        public List<Model.BusinessType> GetType()
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Query<Model.BusinessType>(_select).ToList();
            }
        }

        public List<Model.BusinessType> GetType(int parentid)
        {
            using (var cn = new SqlConnection(SqlConnectString.CONNSTRING))
            {
                return cn.Query<Model.BusinessType>(_selectChild, new { ParentID = parentid }).ToList();
            }
        }
            
    }
}
